/*
 * Copyright (c) 2013-2015 Charkey. All rights reserved.
 *
 * This software is the confidential and proprietary information of Charkey.
 * You shall not disclose such Confidential Information and shall use it only
 * in accordance with the terms of the agreements you entered into with Charkey.
 *
 * Charkey MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE SOFTWARE,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
 *
 * Charkey SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

package cn.simastudio.talos.core.controller;

import cn.simastudio.talos.common.constants.Modules;
import cn.simastudio.talos.common.utils.ExceptionUtils;
import cn.simastudio.talos.core.constants.MessageCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.LocaleResolver;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;

/**
 * Usage: 异常页面，401，404，500
 *
 * @author Charkey
 * @date 2016/5/16 16:49
 */
@Controller
@RequestMapping("/error")
public class ErrorPageController {

    @Autowired
    private MessageSource messageSource;

    @Autowired
    private LocaleResolver localeResolver;

    @RequestMapping(value = {"/400"})
    public String error400(HttpServletRequest request, ModelMap model) {
        // 获取国际化配置文件中code对应的内容，若无相应的code，则抛出NoSuchMessageException；要求：一定要有相应的code配置！
        String localizedMessage = messageSource.getMessage(MessageCode.BAD_REQUEST, null, localeResolver.resolveLocale(request));
        model.addAttribute("module", Modules.SYSTEM);
        model.addAttribute("code", "400");
        model.addAttribute("message", localizedMessage);
        return "/jsp/error/general_error.jsp";
    }

    @RequestMapping(value = {"/401"})
    public String error401(HttpServletRequest request, ModelMap model) {
        // 获取国际化配置文件中code对应的内容，若无相应的code，则抛出NoSuchMessageException；要求：一定要有相应的code配置！
        String localizedMessage = messageSource.getMessage(MessageCode.ERROR_UNAUTHORIZED, null, localeResolver.resolveLocale(request));
        model.addAttribute("module", Modules.AUTH);
        model.addAttribute("code", "401");
        model.addAttribute("message", localizedMessage);
        return "/jsp/error/401.jsp";
    }

    @RequestMapping(value = {"/404"})
    public String error404(HttpServletRequest request, ModelMap model) {
        // 获取国际化配置文件中code对应的内容，若无相应的code，则抛出NoSuchMessageException；要求：一定要有相应的code配置！
        String localizedMessage = messageSource.getMessage(MessageCode.ERROR_NOT_FOUND, null, localeResolver.resolveLocale(request));
        model.addAttribute("module", Modules.SYSTEM);
        model.addAttribute("code", "404");
        model.addAttribute("message", localizedMessage);
        model.addAttribute("develop_message", "Request URI:" + request.getRequestURI());
        return "/jsp/error/404.jsp";
    }

    @RequestMapping(value = {"/500"})
    public String error500(HttpServletRequest request, ModelMap model) {
        Throwable throwable = (Throwable) request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
        // 获取国际化配置文件中code对应的内容，若无相应的code，则抛出NoSuchMessageException；要求：一定要有相应的code配置！
        String localizedMessage = messageSource.getMessage(MessageCode.INTERNAL_SERVER_ERROR, null, localeResolver.resolveLocale(request));
        model.addAttribute("module", Modules.SYSTEM);
        model.addAttribute("code", "500");
        model.addAttribute("message", localizedMessage);
        model.addAttribute("develop_message", ExceptionUtils.getExceptionMessage(throwable));
        return "/jsp/error/500.jsp";
    }

}
